using GEM.Domain.Events.Users;

namespace GEM.Domain.Entities.App;

public class AppUser : EntityBase
{
    public string Username { get; set; } = string.Empty;
    public string Password { get; set; } = string.Empty;

    //用于存储“加密盐值”
    public string Salt { get; set; } = string.Empty;

    //昵称
    public string? Nickname { get; set; }
    //头像
    public string? AvatarUrl { get; set; }
    //邮箱
    public string? Email { get; set; }
    //电话
    public int Phone { get; set; }

    public Guid? DepartmentId { get; set; }
    public AppDepartment? Department { get; set; }

    // 表示当前用户拥有的角色集合
    public ICollection<AppRole> Roles { get; set; } = new List<AppRole>();


    public bool AssignRole(AppRole role)
    {
        //如果不包含则添加角色
        if (!Roles.Contains(role))
        {
            Roles.Add(role);
            return true;
        }
        return false;
    }

    public bool RemoveRole(AppRole role)
    {
        //如果包含则移除角色
        if (Roles.Contains(role))
        {
            Roles.Remove(role);
            return true;
        }
        return false;
    }

    //判断当前用户是否拥有指定编码的权限
    public bool HasPermission(string permissionCode)
    {
        return Roles.Any(r => r.HasPermission(permissionCode));
    }

    // 添加注册方法，触发领域事件，注册后，会自动触发用户注册事件
    public void Register()
    {
        // 添加领域事件
        AddDomainEvent(new UserRegisteredDomainEvent(Id, Username));
    }

}